class Outline{
      constructor(){}
      // 大纲管理 展示数据
      list(req,res){
            let sql='select * from `outlines` order by `sort` asc'
            req.db.query(sql,{
                  type:req.db.QueryTypes.SELECT
            }).then((result)=>{
                  setTimeout(() => {
                        res.send({
                              error:0,
                              message:'success',
                              data:result
                        })  
                  },0);
                  
            }).catch((error)=>{
                  console.log(error)
                  res.send({
                        error:4,
                        message:'query fail'
                  })
            })
      }

      // 大纲管理 添加数据
      add(req,res){
            let {value,pid}=req.body
            let sql='insert into `outlines` set `value`="'+value+'",`pid`="'+pid+'",`sort`="'+(new Date().getTime())+'"'
            req.db.query(sql).then(result=>{
                  setTimeout(() => {
                        res.send({
                              error:0,
                              message:'success'
                        })  
                  }, 0);
            }).catch(error=>{
                  console.log(error)
                  res.send({
                        error:10,
                        message:'insert fail'
                  })
            })
      }

      //大纲管理 删除数据
      delete(req,res){
            let id=req.query.id
            let sql1='select * from `outlines` where `pid`="'+id+'"'
            let sql2='delete from `outlines` where id="'+id+'"'
            req.db.query(sql1,{type:req.db.QueryTypes.SELECT}).then(result=>{
                  if(result.length==0){
                        req.db.query(sql2).then(result=>{
                              setTimeout(()=>{
                                    res.send({
                                          error:0,
                                          message:'success'
                                    })
                              })
                        }).catch(error=>{
                              console.log(error)
                              res.send({
                                    error:20,
                                    message:'delete fail'
                              })
                        })
                  }else{
                        res.send({
                              error:21,
                              message:'There are submenus that cannot be deleted'
                        })
                  }

            }).catch(error=>{
                  console.log(error)
                  res.send({
                        error:40,
                        message:'query fail'
                  })
            })
      }

      // 大纲管理 编辑菜单
      edit(req,res){
            let {value,id}=req.body
            let sql='update `outlines` set `value`="'+value+'" where `id`="'+id+'"'
            req.db.query(sql).then(result=>{
                  setTimeout(()=>{
                        res.send({
                              error:0,
                              message:'success'
                        })
                  })
            }).catch(error=>{
                  console.log(error)
                  res.send({
                        error:30,
                        message:'edit fail'
                  })
            })
      }

      // 大纲管理 菜单排序
      sort(req,res){
            let {id,dir,sort,pid}=req.body
            let sql={
                  up:'select * from `outlines` where `sort`<"'+sort+'" && `pid`="'+pid+'" order by `sort` desc limit 1',
                  down:'select * from `outlines` where  `sort` >"'+sort+'" && `pid`="'+pid+'" order by `sort` asc limit 1'
            }
            req.db.query(sql[dir],{type:req.db.QueryTypes.SELECT}).then(result=>{
                  if(result.length==0){
                        res.send({
                              error:0,
                              message:'success'
                        })
                        return
                  }
                  req.db.query('update `outlines` set `sort`="'+result[0].sort+'" where `id`="'+id+'"').then(()=>{
                        req.db.query('update `outlines` set `sort`="'+sort+'" where `id`="'+result[0].id+'"').then(()=>{
                              res.send({
                                    error:0,
                                    message:'success'
                              })
                        })
                  })
            }).catch(error=>{
                  console.log(result)
                  res.send({
                        error:40,
                        message:'select fail'
                  })
            })
      }
}
module.exports=new Outline()